'use strict';

module.exports = app => {
  class TagController extends app.Controller {
    async index (ctx) {
      let data = await app.mysql.get('tag')
      console.info('data', data)
      ctx.body = 'data body4'
      // ctx.helper.rest.resp({ctx, code: 0, message: 'info', data: 'data' })
    }

    async show (ctx) {
      ctx.body = 'data body1'
      // ctx.helper.rest.resp({ctx, code: 0, message: 'info', data: 'data' })
    }

    async logined (ctx) {
      let result = await app.httpclient.curl('http://localhost:10001/v1/user/info', {
        dataType: 'json',
        beforeRequest: ctx.helper.rest.genProxyAdmin({ctx})
      }).catch(ctx.helper.genAppError({ctx}))

      let { data } = result || {}
      if (data && data.error_code === 0) {
        ctx.body = result.data
      } else {
        ctx.helper.abortOnError({ctx, error: ctx.errors.UNLOGINED})
      }
    }

    async registration (ctx) {
      let { email = '', username = '', password = '' } = ctx.request.body

      email = email.toString()
      username = username.toString()
      password = password.toString()

      let [error] = ctx.app.validator.validate({
        email: 'email',
        username: ctx.validation.reg_username,
        password: ctx.validation.password
      }, {email, username, password}) || []

      if (ctx.helper.abortOnError({ctx, error})) {
        return
      }

      let result = await app.httpclient.curl('http://localhost:10001/v1/registration', {
        method: 'POST',
        dataType: 'json',
        contentType: 'json',
        data: {
          user_name: username,
          password: password,
          email: email
        },
        beforeRequest: ctx.helper.rest.genProxyAdmin({ctx})
      }).catch(ctx.helper.genAppError({ctx}))

      if (!result) {
        return ctx.helper.abortOnError({ctx, error: ctx.errors.REGISTER_FAIL})
      }

      let { data } = result

      if (!data) {
        return ctx.helper.abortOnError({ctx, error: ctx.errors.REGISTER_FAIL})
      }

      ctx.helper.rest.AuthProxyAdmin({ctx, set_cookie: result.headers['set-cookie']})
      ctx.helper.rest.resp({ctx, code: data.error_code, message: data.info, data: data.data })
    }

    async login (ctx) {
      let { username, password, remember_me = false } = ctx.request.body

      let [error] = ctx.app.validator.validate({
        username: ctx.validation.username,
        password: ctx.validation.password
      }, {username, password}) || []

      if (ctx.helper.abortOnError({ctx, error})) {
        return
      }

      let result = await app.httpclient.curl('http://localhost:10001/v1/login', {
        method: 'POST',
        dataType: 'json',
        contentType: 'json',
        data: {
          user_name: username,
          password: password
        },
        beforeRequest: ctx.helper.rest.genProxyAdmin({ctx})
      }).catch(ctx.helper.genAppError({ctx}))

      if (!result) {
        return ctx.helper.abortOnError({ctx, error: ctx.errors.LOGIN_FAIL})
      }

      let { data = {} } = result

      ctx.helper.rest.AuthProxyAdmin({ctx, set_cookie: result.headers['set-cookie']})
      ctx.helper.rest.resp({ctx, code: data.error_code, message: data.info, data: data.data})
    }

    // just proxy it
    async logout (ctx) {
      let result = await app.httpclient.curl('http://localhost:10001/v1/logout', {
        method: 'POST',
        beforeRequest: ctx.helper.rest.genProxyAdmin({ctx})
      }).catch(ctx.helper.genAppError({ctx}))

      ctx.helper.rest.AuthProxyAdmin({ctx, set_cookie: result.headers['set-cookie']})
      ctx.helper.rest.resp({ctx, message: `you have logout.`})
    }
  }

  return TagController
}
